package handler

import (
	"github.com/gin-gonic/gin"
	jwt "github.com/golangblogs/gojwt"
	"main/api/request"
	"main/internal/dao"
	"main/internal/model"
	"net/http"
)

func Register(c *gin.Context) {
	var req request.RegisterReq
	data := model.User{
		Username: req.Username,
		Password: req.Password,
	}

	if !dao.Create(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": data,
	})
}

func Login(c *gin.Context) {
	var req request.LoginReq
	data := model.User{
		Username: req.Username,
		Password: req.Password,
	}
	if req.Username != data.Username || req.Password != data.Password {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "用户名或密码错误",
			"data": nil,
		})
		return
	}
	token := jwt.CustomClaims{
		ID: uint(req.Id),
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}
